package service

import (
	"cs-backend/db"
	"cs-backend/dto"
	"cs-backend/model"
)

type UserService struct {
}

func (s *UserService) GetById(id int64) dto.ApiResponse {
	user := new(model.User)
	//user.Base.Id = id
	//return s.BaseService.GetById(user)
	has, err := db.GetEngine().ID(id).Get(user)
	if err != nil {
		return dto.ResponseInternalServerError()
	}
	if has {
		return dto.ResponseSuccess(user)
	} else {
		return dto.ResponseNotFound()
	}

}

func (s *UserService) ListAll() dto.ApiResponse {
	datalist := make([]model.User, 0)
	err := db.GetEngine().Find(&datalist)
	if err != nil {
		return dto.ResponseInternalServerError()
	}
	return dto.ResponseSuccess(datalist)
}
func (s *UserService) ListPage() dto.ApiResponse {
	datalist := make([]model.User, 0)
	err := db.GetEngine().Find(&datalist)
	if err != nil {
		return dto.ResponseInternalServerError()
	}
	return dto.ResponseSuccess(datalist)
}
func (s *UserService) SqlMapQuery() dto.ApiResponse {
	sqlKey := "sql_user_01"
	results, err := db.GetEngine().SqlMapClient(sqlKey).Query().List()
	if err != nil {
		return dto.ResponseInternalServerError()
	}
	return dto.ResponseSuccess(results)
}

func (s *UserService) UpdateColumns(user *model.User, columns []string) dto.ApiResponse {
	affected, err := db.GetEngine().ID(user.Id).MustCols(columns...).Update(user)
	if err != nil {
		return dto.ResponseInternalServerError()
	}
	if affected <= 0 {
		return dto.ResponseNotFound()
	}
	return dto.ResponseSuccess(user)
}
